define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'express/package/index?batch_id=' + Config.batch_id,
                    add_url: 'express/package/add',
                    edit_url: 'express/package/edit',
                    del_url: 'express/package/del',
                    multi_url: 'express/package/multi',
                    import_url: 'express/package/import',
                    table: 'express_package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //启用跨页选择
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: '用户',operate:'LIKE',formatter:function (value,row,index) {
                                return row.user ? "<a href=\"javascript:void(0)\" data-tips-image='"+Fast.api.cdnurl(row.user.avatar)+"'><img class=\"img-sm img-center\" src='"+Fast.api.cdnurl(row.user.avatar)+"'></a>&nbsp;" + value : '';
                            }},
                        {field: 'address.name', title: __('收件人'),operate:'LIKE'},
                        {field: 'address.idcard', title: __('身份证号'),operate:'LIKE'},
                        {field: 'expressorder.order_no', title: __('Expressorder.order_no')},
                        {field: 'package_no', title: __('Package_no')},
                        {field: 'express_no', title: __('Express_no')},
                        {field: 'delivery_price', title: __('Delivery_price'), operate:'BETWEEN'},
                        {field: 'delivery_time', title: __('Delivery_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('Status 7'),"8":__('Status 8'),"9":__('Status 9')}, formatter: Table.api.formatter.status},
                        //{field: 'delivery_status', title: __('Delivery_status'), searchList: {"no":__('Delivery_status no'),"yes":__('Delivery_status yes')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table,buttons: [

                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-warning btn-detail btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'express/package/detail',
                                    visible: function (row) {
                                        if(row.express_no){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: __('发货'),
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-success btn-editone  btn-dialog',
                                    icon: 'fa fa-truck ',
                                    url: 'express/package/edit',
                                    visible: function (row) {
                                        if(!row.express_no){
                                            return true;
                                        }else{
                                            return false;
                                        }

                                    }
                                },

                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            $(document).on("click", ".btn-addBatch", function () {
                var that = this;
                var id = "addBatchTpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "加入批次",
                    area: ["500px", "300px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        var ids = Table.api.selectedids(table);
                        $('#c-ids').val(ids.join(','));
                        Form.api.bindevent(form, function (data) {
                            $(".btn-refresh").trigger("click");
                            Layer.closeAll();
                        });
                    }
                });
            });
            $(document).on("click", "input[name='row[type]']", function () {
                var type = $(this).val();
                if(type == 'old'){
                    $('.batch-select').removeClass('hide')
                }else{
                    $('.batch-select').addClass('hide')
                }
            });

            $(document).on("click", ".btn-invoice", function () {
                if($(this).data('id')){
                    Backend.api.open('express/package/print/ids/' + $(this).data('id'), __('打印发货单'),{area:['80%', '80%']});
                }else{
                    Backend.api.open('express/package/print/ids/' + Table.api.selectedids(table), __('打印发货单'),{area:['80%', '80%']});
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table',function(data){
                $(".btn-editone").data("area", ["90%","80%"]);
                $(".btn-add").data("area", ["90%","80%"]);
                $(".btn-edit").data("area", ["90%","80%"]);
                $(".btn-detail").data("area", ["90%","80%"]);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-express_com", function () {
                var code = $(this).val();
                Fast.api.ajax({
                    url: 'express/express/find',
                    data:{
                        code:code
                    }
                },function (ret) {
                    $('#c-express_no').val(ret)
                    return false;
                },function (ret) {
                    $('#c-express_no').val('')
                })
            })
            Controller.api.bindevent();
        },
        detail:function(){
            $(document).on("click", ".btn-invoice", function () {
                if($(this).data('id')){
                    Backend.api.open('express/package/print/ids/' + $(this).data('id'), __('打印发货单'),{area:['80%', '80%']});
                }else{
                    Backend.api.open('express/package/print/ids/' + Table.api.selectedids(table), __('打印发货单'),{area:['80%', '80%']});
                }
            });
            $(document).on("click", ".btn-subscribe", function () {
                var id = $(this).data('id');
                var loadding = Layer.load();
                Fast.api.ajax({
                    url: 'express/package/subscribe',
                    data:{
                        'id':id
                    }
                },function (ret) {
                    Layer.close(loadding);
                    window.location.reload();
                })
            });
            $(document).on("click", ".btn-track", function () {
                Backend.api.open('express/package/track/ids/' + $(this).data('id'), __('物流轨迹'),{area:['80%', '80%']});
            });
            $(document).on("click",'.edit-weight',function () {
                var id = $(this).data('id');
                var unit = $(this).data('unit');
                layer.prompt({title: '请重新输入(单位' + unit + ')', formType: 3}, function(weight, index){
                    var loadding = Layer.load();
                    Fast.api.ajax({
                        url: 'express/package/editWeight',
                        data:{
                            id:id,
                            weight:weight
                        }
                    },function (ret) {
                        Layer.close(loadding);
                        window.location.reload();
                    })
                });
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});